package notice

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"github.com/hwUltra/fb-tools/gormx"
	"github.com/hwUltra/fb-tools/jwtx"

	"github.com/zeromicro/go-zero/core/logx"
)

type MyPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMyPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MyPageLogic {
	return &MyPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MyPageLogic) MyPage(in *types.SysNoticePageReq) (resp *types.SysNoticePageResp, err error) {
	uid := jwtx.NewJwtX(l.ctx, l.svcCtx.Config.JwtUserKey).GetUid()
	snModel := model.SysNoticeModel{}
	total := int64(0)
	if err := l.svcCtx.GormClient.GormDb.Model(snModel).
		Scopes(
			snModel.WithCreatedAt(in.StartTime, in.EndTime),
			snModel.WithTitle(in.Title),
			snModel.WithUid(uid),
			snModel.WithStatus(in.Status)).
		Count(&total).Error; err != nil {
		return nil, err
	}
	list := make([]*types.SysNotice, 0)
	if total > 0 {
		l.svcCtx.GormClient.GormDb.Model(snModel).
			Scopes(
				snModel.WithCreatedAt(in.StartTime, in.EndTime),
				snModel.WithTitle(in.Title),
				snModel.WithUid(uid),
				snModel.WithStatus(in.Status),
				gormx.Paginate(int(in.PageNum), int(in.PageSize))).
			Order("id desc").Find(&list)
	}

	return &types.SysNoticePageResp{
		Total: total,
		List:  list,
	}, nil

}
